extern void listDir(fs::FS&, const char*, uint8_t);
extern void alarmmusic(String);
extern int espsleep(int);
string picName[50] = {};  // 存储图片的名字
string picSize[50] = {};  // 存储图片的大小
void picmod(const char* dirname, uint8_t levels) {
  for (int i = 0; i < 50; i++) picName[i] = "";
  display.fillScreen(BackC);
  File root = FFat.open(dirname);
  if (!root) {
    Serial.println("Failed to open directory");
    return;
  }
  if (!root.isDirectory()) {
    Serial.println("Not a directory");
    return;
  }

  //File txtfile = root.openNextFile();
  int i = 0;
  //File root = FFat.open(dirname);
  File txtfile = root.openNextFile();
  while (txtfile) {
    if (txtfile.isDirectory()) {
      Serial.print("  DIR : ");
      Serial.println(txtfile.name());
      if (levels) {
        listDir(FFat, txtfile.path(), levels - 1);
      }
    } else {
      if (i < 50) {
        string fname(txtfile.name());
        if (String(txtfile.name()).substring(0, 3) == "[p]") {
          picName[i] = txtfile.name();
          picSize[i] = std::__cxx11::to_string(((txtfile.size() / 1024) > 1023) ? (txtfile.size() / (1024 * 1024)) : (txtfile.size() / 1024)) + (((txtfile.size() / 1024) > 1023) ? "MB" : "KB");
          i++;
        }
      }
    }
    txtfile = root.openNextFile();
  }
  root.close();

  int m = 0;
  int n = 0;
  display.drawRoundRect(0, 18, 200, 18, 20, FormC);  //画外框
  while (1) {

    (m > 1) ? m : 1;
    for (int i = 0; i < 4; i++) {
      string fname = picName[5 * m + i];
      string fsize = picSize[5 * m + i];
      string f = fname + "   " + fsize;
      const char* ff = f.c_str();
      centerx(ff, 30 + i * 20);
    }

    String p = "第" + String(m + 1) + "页" + "  返回";
    centerx(p, 30 + 4 * 20);
    nextPage(10, -1);
    n = 0;
    bool left = 0;
    while (1) {
      espsleep(1);
      if (tapcheck(1) && !tapcheck(3)) {
        n = (n == 4) ? 0 : n + 1;
        display.drawRoundRect(0, 18 + 20 * ((n == 0) ? 4 : (n - 1)), 200, 18, 20, BackC);  //画外框
        display.drawRoundRect(0, 18 + 20 * n, 200, 18, 20, FormC);                         //画外框
        nextPage(10, -1);
      }
      if (tapcheck(2)) {
        delay(200);
        if (tapcheck(2) && n < 4) {
          centerx("按确认键删除，按任意键退出", 60, 1, 0, 200, "否");
          nextPage(10, -1);
          espsleep(1);
          if (tapcheck(1) || tapcheck(3)) {
            display.firstPage();
            display.fillScreen(BackC);
            break;
          }
          if (tapcheck(2)) {
            FFat.remove(("/" + picName[5 * m + n]).c_str());  //删除
            //FFat.remove(("/[r]" + picName[5 * m + n]).c_str());  //删除
            display.firstPage();
            display.fillScreen(BackC);
            break;
          }
        } else if (!tapcheck(1) && !tapcheck(3) && !tapcheck(3)) {
          if (n < 4) {
            while (1) {
              picfile = FFat.open(("/" + picName[5 * m + n]).c_str(), FILE_READ);
              picout = picfile.readString();
              // Serial.println("picout");
              // Serial.println(picout);
              // Serial.println("picout");
              picfile.close();
              display.fillScreen(BackC);
              drawScale(picout, 0, 0, 200, 200, 1, 1, false);
              nextPage(10, -1);
              espsleep(0);
              if (tapcheck(2)) break;
            }
            //Readtxt(("/" + picName[5 * m + n]).c_str());
            display.firstPage();
            display.drawRoundRect(0, 18, 200, 18, 20, FormC);
            break;
          } else if (n == 4) {
            left = 1;
            break;
          }
        }
      }

      if (tapcheck(3)) {
        delay(200);
        if (tapcheck(3)) {
          m += 1;
          display.fillScreen(BackC);
          display.drawRoundRect(0, 18, 200, 18, 20, FormC);
          break;  //画外框  break;
        } else {
          m = (m == 0) ? m : m - 1;
          display.fillScreen(BackC);
          display.drawRoundRect(0, 18, 200, 18, 20, FormC);
          break;  //画外框break;
        }
      }
    }
    if (left == 1) break;
    display.firstPage();
    display.fillScreen(BackC);
  }
  //homesetup();
}

string musicName[50] = {};  // 存储音频的名字
string musicSize[50] = {};  // 存储音频的大小
void musicmod(const char* dirname, uint8_t levels) {
  for (int i = 0; i < 50; i++) musicName[i] = "";
  display.fillScreen(BackC);
  File root = FFat.open(dirname);
  if (!root) {
    Serial.println("Failed to open directory");
    return;
  }
  if (!root.isDirectory()) {
    Serial.println("Not a directory");
    return;
  }

  //File txtfile = root.openNextFile();
  int i = 0;
  //File root = FFat.open(dirname);
  File txtfile = root.openNextFile();
  while (txtfile) {
    if (txtfile.isDirectory()) {
      Serial.print("  DIR : ");
      Serial.println(txtfile.name());
      if (levels) {
        listDir(FFat, txtfile.path(), levels - 1);
      }
    } else {
      if (i < 50) {
        string fname(txtfile.name());
        if (String(txtfile.name()).substring(0, 3) == "[m]") {
          musicName[i] = txtfile.name();
          musicSize[i] = std::__cxx11::to_string(((txtfile.size() / 1024) > 1023) ? (txtfile.size() / (1024 * 1024)) : (txtfile.size() / 1024)) + (((txtfile.size() / 1024) > 1023) ? "MB" : "KB");
          i++;
        }
      }
    }
    txtfile = root.openNextFile();
  }
  root.close();

  int m = 0;
  int n = 0;
  display.drawRoundRect(0, 18, 200, 18, 20, FormC);  //画外框
  while (1) {

    (m > 1) ? m : 1;
    for (int i = 0; i < 4; i++) {
      string fname = musicName[5 * m + i];
      string fsize = musicSize[5 * m + i];
      string f = fname + "   " + fsize;
      const char* ff = f.c_str();
      centerx(ff, 30 + i * 20);
    }

    String p = "第" + String(m + 1) + "页" + "  返回";
    centerx(p, 30 + 4 * 20);
    nextPage(10, -1);
    n = 0;
    bool left = 0;
    while (1) {
      espsleep(1);
      if (tapcheck(1) && !tapcheck(3)) {
        n = (n == 4) ? 0 : n + 1;
        display.drawRoundRect(0, 18 + 20 * ((n == 0) ? 4 : (n - 1)), 200, 18, 20, BackC);  //画外框
        display.drawRoundRect(0, 18 + 20 * n, 200, 18, 20, FormC);                         //画外框
        nextPage(10, -1);
      }
      if (tapcheck(2)) {
        delay(200);
        if (tapcheck(2) && n < 4) {
          centerx("按确认键删除，按任意键退出", 60, 1, 0, 200, "否");
          nextPage(10, -1);
          espsleep(1);
          if (tapcheck(1) || tapcheck(3)) {
            display.firstPage();
            display.fillScreen(BackC);
            break;
          }
          if (tapcheck(2)) {
            FFat.remove(("/" + musicName[5 * m + n]).c_str());  //删除
            //FFat.remove(("/[r]" + picName[5 * m + n]).c_str());  //删除
            display.firstPage();
            display.fillScreen(BackC);
            break;
          }
        } else if (!tapcheck(1) && !tapcheck(3) && !tapcheck(3)) {
          if (n < 4) {
            while (1) {
              File musicfile;
              String musicout;
              musicfile = FFat.open(("/" + musicName[5 * m + n]).c_str(), FILE_READ);
              musicout = musicfile.readString();
              musicfile.close();
              centerx(musicout, 60, 1, 0, 200, "否");
              nextPage();
              if (musicout.length() != 0) alarmmusic(musicout);
              else {
                centerx("空文件", 60, 1, 0, 200, "否");
              }
              if (tapcheck(2)) break;
            }
            //Readtxt(("/" + picName[5 * m + n]).c_str());
            display.firstPage();
            display.drawRoundRect(0, 18, 200, 18, 20, FormC);
            break;
          } else if (n == 4) {
            left = 1;
            break;
          }
        }
      }

      if (tapcheck(3)) {
        delay(200);
        if (tapcheck(3)) {
          m += 1;
          display.fillScreen(BackC);
          display.drawRoundRect(0, 18, 200, 18, 20, FormC);
          break;  //画外框  break;
        } else {
          m = (m == 0) ? m : m - 1;
          display.fillScreen(BackC);
          display.drawRoundRect(0, 18, 200, 18, 20, FormC);
          break;  //画外框break;
        }
      }
    }
    if (left == 1) break;
    display.firstPage();
    display.fillScreen(BackC);
  }
  //homesetup();
}
extern void txtmod(const char*, uint8_t);

#include "ff.h"
String getRestSize() {
  DWORD free_clusters;
  FRESULT fatfsres;
  // 获取剩余空间信息
  fatfsres = f_getfree("", &free_clusters, NULL);
  if (fatfsres == FR_OK) {
    // 使用 free_clusters 和 fs.csize 计算剩余空间大小
    DWORD total_bytes = (FFat.csize * FFat.sectorsize);
    DWORD free_bytes = (free_clusters * FFat.csize * FFat.sectorsize);
    // 打印剩余空间信息
    printf("Total Space: %d bytes\n", total_bytes);
    printf("Free Space: %d bytes\n", free_bytes);

    return std::__cxx11::to_string(((free_bytes / 1024) > 1023) ? (free_bytes / (1024 * 1024)) : (free_bytes / 1024)) + (((free_bytes / 1024) > 1023) ? "MB" : "KB");
  }
}


void filedesk() {
  int setselect = 0;
  display.firstPage();
  display.fillScreen(BackC);
  while (1) {
String restsize=getRestSize();
centerx(restsize,20);
    display.firstPage();
    display.fillScreen(BackC);
    display.drawInvertedBitmap(xshifting, yshifting, fileback, 200, 200, FormC);
    if (setselect != 0) {
      display.drawRoundRect(90 + xshifting, 50 + yshifting + setselect * 47, 20, 3, 1, FormC);
      display.drawRoundRect(90 + xshifting, 50 + yshifting + (setselect - 1) * 47, 20, 3, 1, BackC);
    } else {
      display.drawRoundRect(90 + xshifting, 50 + yshifting + setselect * 47, 20, 3, 1, FormC);
      display.drawRoundRect(90 + xshifting, 50 + yshifting + 3 * 47, 20, 3, 1, BackC);
    }
    nextPage(10, -1);
    espsleep(1);
    if (tapcheck(1)) setselect = (setselect == 3) ? 0 : setselect + 1;
    if (tapcheck(2)) {
      switch (setselect) {
        case 0:
          txtmod("/", 0);
          break;
        case 1:
          picmod("/", 0);
          break;
        case 2: musicmod("/", 0); break;
        case 3: return;
      }
    }
  }
  nextPage(10, -1);
}
